<?php
session_start();
function success(){
    $result = [
        'error'=> 0,
        'message' => '成功'
    ];
    return json_encode($result);
}
function error($msg){
    $data = [
        'error'=> 1,
        'message' => $msg
    ];
    return json_encode($data);
}
$username = $_SESSION['user'];
$textarea = $_POST['textarea'];

try{
    $db = new PDO('mysql:dbname=qq;host=127.0.0.1','root','123456');
    $statement = $db ->prepare('insert into history set name=:username,text=:textarea,time=:time');
    $state = $statement->execute([':username'=>$username,':textarea'=>$textarea,':time'=>date('y-m-d h:m:s')]);

    if($state){
        echo success();
    }else{

        echo error('发送失败！');
    }
}catch (PDOException $e){
    echo $e -> getMessage();
}


